home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 21 / CU Amiga Magazine's Super CD-ROM 21 (1998)(EMAP Images)(GB)[!][issue 1998-04].iso / CUCD / Graphics / FlashMandel / Sources / Modules / ColorWindow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-25  |  27.2 KB  |  719 lines

  1. /*******************************************************************************
  2. **
  3. **  Coded by Dino Papararo                 27-Oct-1997
  4. **
  5. *******************************************************************************/
  6.  
  7. #define __USE_SYSBASE
  8.  
  9. #include <math.h>
  10. #include <string.h>
  11. #include <exec/types.h>
  12. #include <exec/execbase.h>
  13. #include <intuition/intuition.h>
  14. #include <intuition/gadgetclass.h>
  15. #include <proto/exec.h>
  16. #include <proto/intuition.h>
  17. #include <proto/graphics.h>
  18. #include <proto/gadtools.h>
  19. #include <proto/utility.h>
  20.  
  21. #define PALETTE      (0L)
  22. #define ACCEPT       (1L)
  23. #define RESET        (2L)
  24. #define CANCEL       (3L)
  25. #define RED          (4L)
  26. #define GREEN        (5L)
  27. #define BLUE         (6L)
  28. #define COPY         (7L)
  29. #define SWAP         (8L)
  30. #define SPREAD       (9L)
  31. #define UNDO         (10L)
  32.  
  33. #define STARTPEN     (4L)
  34.  
  35. BOOL ModifyPalette  (struct Window *,WORD,WORD,WORD,WORD,ULONG *);
  36. VOID KeepPalette    (struct Window *);
  37. VOID Copy           (struct Window *,ULONG);
  38. VOID Paste          (struct Window *,const ULONG);
  39. VOID Swap           (struct Window *,const ULONG,const ULONG);
  40. BOOL Spread         (struct Window *,const ULONG,const ULONG);
  41.  
  42. IMPORT struct ExecBase *SysBase;
  43.  
  44. ULONG COLOR_RGB [3L * 256L + 2L],UNDO_RGB [3L * 256L + 2L],COPY_RGB [3L];
  45.  
  46. struct NewGadget BUTTON_GAD,SLIDER_GAD,PALETTE_GAD;
  47.  
  48. BOOL ModifyPalette (struct Window *Win,WORD LeftEdge,WORD TopEdge,WORD Width,WORD Height,ULONG *Palette32)
  49. {
  50. struct Window *ColorWin;
  51.  
  52. struct IntuiMessage *Message;
  53.  
  54. struct Gadget *GadList = NULL,*MyButtonGad,*MyPaletteGad;
  55.  
  56. struct Gadget *RedSliderGad,*GreenSliderGad,*BlueSliderGad,*MyGad = NULL;
  57.  
  58. BOOL Copy_Msg = FALSE,Swap_Msg = FALSE,Spread_Msg = FALSE,Exit = FALSE;
  59.  
  60. ULONG SelectedPen = STARTPEN,OldPen = NULL,NewPen;
  61.  
  62. UWORD MyCode = 0,Min_Value = 0,Max_Value = 255;
  63.  
  64. ULONG MyClass = NULL,Colors,ColorBase = 3L * STARTPEN + 1L;
  65.  
  66. ULONG RedLevel,GreenLevel,BlueLevel;
  67.  
  68. CPTR *VInfo;
  69.  
  70.   Colors = 1L << Win->RPort->BitMap->Depth;
  71.  
  72.   if (Colors < 2L) return FALSE;
  73.  
  74.   VInfo = GetVisualInfo (Win->WScreen,TAG_END);
  75.  
  76.   if (! VInfo) return FALSE;
  77.  
  78.   COLOR_RGB [0L] = UNDO_RGB [0L] = Colors << 16L;
  79.  
  80.   COLOR_RGB [3L * Colors + 1L] = UNDO_RGB [3L * Colors + 1L] = NULL;
  81.  
  82.   COPY_RGB [0L] = COPY_RGB [1L] = COPY_RGB [2L] = NULL;
  83.  
  84.   GetRGB32 (ViewPortAddress (Win)->ColorMap,0L,Colors,&COLOR_RGB [1L]);
  85.  
  86.   RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  87.  
  88.   GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  89.  
  90.   BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  91.  
  92.   MyButtonGad = CreateContext (&GadList);
  93.  
  94.   BUTTON_GAD.ng_VisualInfo = PALETTE_GAD.ng_VisualInfo = SLIDER_GAD.ng_VisualInfo = VInfo;
  95.  
  96.   BUTTON_GAD.ng_LeftEdge   = 10;
  97.  
  98.   BUTTON_GAD.ng_TopEdge    = 147;
  99.  
  100.   BUTTON_GAD.ng_Width      = 80;
  101.  
  102.   BUTTON_GAD.ng_Height     = 20;
  103.  
  104.   BUTTON_GAD.ng_GadgetText = "_Accept";
  105.  
  106.   BUTTON_GAD.ng_GadgetID   = ACCEPT;
  107.  
  108.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  109.  
  110.   BUTTON_GAD.ng_LeftEdge  += (15 + BUTTON_GAD.ng_Width);
  111.  
  112.   BUTTON_GAD.ng_GadgetText = "_Reset";
  113.  
  114.   BUTTON_GAD.ng_GadgetID   = RESET;
  115.  
  116.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  117.  
  118.   BUTTON_GAD.ng_LeftEdge  += (15 + BUTTON_GAD.ng_Width);
  119.  
  120.   BUTTON_GAD.ng_GadgetText = "_Cancel";
  121.  
  122.   BUTTON_GAD.ng_GadgetID   = CANCEL;
  123.  
  124.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  125.  
  126.   BUTTON_GAD.ng_TopEdge    = 5;
  127.  
  128.   BUTTON_GAD.ng_GadgetText = "C_opy";
  129.  
  130.   BUTTON_GAD.ng_GadgetID   = COPY;
  131.  
  132.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  133.  
  134.   BUTTON_GAD.ng_TopEdge   += (6 + BUTTON_GAD.ng_Height);
  135.  
  136.   BUTTON_GAD.ng_GadgetText = "S_wap";
  137.  
  138.   BUTTON_GAD.ng_GadgetID   = SWAP;
  139.  
  140.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  141.  
  142.   BUTTON_GAD.ng_TopEdge   += (6 + BUTTON_GAD.ng_Height);
  143.  
  144.   BUTTON_GAD.ng_GadgetText = "_Spread";
  145.  
  146.   BUTTON_GAD.ng_GadgetID   = SPREAD;
  147.  
  148.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  149.  
  150.   BUTTON_GAD.ng_TopEdge   += (6 + BUTTON_GAD.ng_Height);
  151.  
  152.   BUTTON_GAD.ng_GadgetText = "_Undo";
  153.  
  154.   BUTTON_GAD.ng_GadgetID   = UNDO;
  155.  
  156.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  157.  
  158.   SLIDER_GAD.ng_LeftEdge    = 25;
  159.  
  160.   SLIDER_GAD.ng_TopEdge     = 110;
  161.  
  162.   SLIDER_GAD.ng_Width       = 225;
  163.  
  164.   SLIDER_GAD.ng_Height      = 8;
  165.  
  166.   SLIDER_GAD.ng_GadgetText  = "R";
  167.  
  168.   SLIDER_GAD.ng_GadgetID    = RED;
  169.  
  170.   RedSliderGad = CreateGadget (SLIDER_KIND,MyButtonGad,&SLIDER_GAD,GA_RelVerify,TRUE,GTSL_Max,Max_Value,GTSL_Level,(WORD) RedLevel,GTSL_LevelFormat,"%03ld",GTSL_MaxLevelLen,3,GTSL_LevelPlace,PLACETEXT_RIGHT,TAG_END);
  171.  
  172.   SLIDER_GAD.ng_TopEdge    += (4 + SLIDER_GAD.ng_Height);
  173.  
  174.   SLIDER_GAD.ng_GadgetText  = "G";
  175.  
  176.   SLIDER_GAD.ng_GadgetID    = GREEN;
  177.  
  178.   GreenSliderGad = CreateGadget (SLIDER_KIND,RedSliderGad,&SLIDER_GAD,GA_RelVerify,TRUE,GTSL_Max,Max_Value,GTSL_Level,(WORD) GreenLevel,GTSL_LevelFormat,"%03ld",GTSL_MaxLevelLen,3,GTSL_LevelPlace,PLACETEXT_RIGHT,TAG_END);
  179.  
  180.   SLIDER_GAD.ng_TopEdge    += (4 + SLIDER_GAD.ng_Height);
  181.  
  182.   SLIDER_GAD.ng_GadgetText  = "B";
  183.  
  184.   SLIDER_GAD.ng_GadgetID    = BLUE;
  185.  
  186.   BlueSliderGad = CreateGadget (SLIDER_KIND,GreenSliderGad,&SLIDER_GAD,GA_RelVerify,TRUE,GTSL_Max,Max_Value,GTSL_Level,(WORD) BlueLevel,GTSL_LevelFormat,"%03ld",GTSL_MaxLevelLen,3,GTSL_LevelPlace,PLACETEXT_RIGHT,TAG_END);
  187.  
  188.   PALETTE_GAD.ng_LeftEdge   = 10;
  189.  
  190.   PALETTE_GAD.ng_TopEdge    = 5;
  191.  
  192.   PALETTE_GAD.ng_Width      = 185;
  193.  
  194.   PALETTE_GAD.ng_Height     = 100;
  195.  
  196.   PALETTE_GAD.ng_GadgetID   = PALETTE;
  197.  
  198.   MyPaletteGad = CreateGadget (PALETTE_KIND,BlueSliderGad,&PALETTE_GAD,GTPA_Color,4,GTPA_IndicatorWidth,10,GTPA_NumColors,(UWORD) Colors,TAG_END);
  199.  
  200.   if (! MyPaletteGad)
  201.   {
  202.      FreeGadgets (GadList);
  203.  
  204.      FreeVisualInfo (VInfo);
  205.  
  206.      return FALSE;
  207.   }
  208.  
  209.   ColorWin = OpenWindowTags (0,WA_Left,LeftEdge,
  210.                                WA_Top,TopEdge,
  211.                                WA_Width,Width,
  212.                                WA_Height,Height,
  213.                                WA_Title,"Palette requester",
  214.                                WA_ScreenTitle,"Modify palette...",
  215.                                WA_CustomScreen,Win->WScreen,
  216.                                WA_IDCMP,IDCMP_CLOSEWINDOW|IDCMP_REFRESHWINDOW|IDCMP_VANILLAKEY|IDCMP_GADGETDOWN|BUTTONIDCMP|SLIDERIDCMP|PALETTEIDCMP,
  217.                                WA_Flags,WFLG_ACTIVATE|WFLG_DRAGBAR|WFLG_SIMPLE_REFRESH|WFLG_RMBTRAP|WFLG_GIMMEZEROZERO,
  218.                                WA_Gadgets,GadList,
  219.                                TAG_END);
  220.  
  221.   if (! ColorWin)
  222.   {
  223.      FreeGadgets (GadList);
  224.  
  225.      FreeVisualInfo (VInfo);
  226.  
  227.      return FALSE;
  228.   }
  229.  
  230.   GT_RefreshWindow (ColorWin,NULL);
  231.  
  232.   do { WaitPort (ColorWin->UserPort);
  233.  
  234.        if (Message = (struct IntuiMessage *) GT_GetIMsg (ColorWin->UserPort))
  235.        {
  236.            MyGad   = (struct Gadget *) Message->IAddress;
  237.  
  238.            MyClass = Message->Class;
  239.  
  240.            MyCode  = Message->Code;
  241.  
  242.            GT_ReplyIMsg ((struct IntuiMessage *) Message);
  243.        }
  244.  
  245.        switch (MyClass)
  246.        {
  247.               case IDCMP_REFRESHWINDOW : GT_BeginRefresh (ColorWin);
  248.  
  249.                                          GT_EndRefresh   (ColorWin,TRUE);
  250.  
  251.                                          break;
  252.  
  253.               case IDCMP_VANILLAKEY    : switch (ToUpper (MyCode))
  254.                                          {
  255.                                                  case 'A' : GetRGB32 (ViewPortAddress (ColorWin)->ColorMap,0L,Colors,&Palette32 [1L]);
  256.  
  257.                                                             Exit = TRUE;
  258.  
  259.                                                             break;
  260.  
  261.                                                  case 'R' : KeepPalette (Win);
  262.  
  263.                                                             LoadRGB32 (ViewPortAddress (ColorWin),Palette32);
  264.  
  265.                                                             GetRGB32 (ViewPortAddress (ColorWin)->ColorMap,0L,Colors,&COLOR_RGB [1L]);
  266.  
  267.                                                             RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  268.  
  269.                                                             GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  270.  
  271.                                                             BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  272.  
  273.                                                             GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  274.  
  275.                                                             GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  276.  
  277.                                                             GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  278.  
  279.                                                             break;
  280.  
  281.                                                  case 'C' : LoadRGB32 (ViewPortAddress (ColorWin),Palette32);
  282.  
  283.                                                             Exit = TRUE;
  284.  
  285.                                                             break;
  286.  
  287.                                                  case 'O' : Copy (ColorWin,SelectedPen);
  288.  
  289.                                                             Copy_Msg = TRUE;
  290.  
  291.                                                             Swap_Msg = FALSE;
  292.  
  293.                                                             Spread_Msg = FALSE;
  294.  
  295.                                                             break;
  296.  
  297.                                                  case 'W' : OldPen = SelectedPen;
  298.  
  299.                                                             Copy_Msg = FALSE;
  300.  
  301.                                                             Swap_Msg = TRUE;
  302.  
  303.                                                             Spread_Msg = FALSE;
  304.  
  305.                                                             break;
  306.  
  307.                                                  case 'S' : OldPen = SelectedPen;
  308.  
  309.                                                             Copy_Msg = FALSE;
  310.  
  311.                                                             Swap_Msg = FALSE;
  312.  
  313.                                                             Spread_Msg = TRUE;
  314.  
  315.                                                             break;
  316.  
  317.                                                  case 'U' : LoadRGB32 (ViewPortAddress (ColorWin),UNDO_RGB);
  318.  
  319.                                                             GetRGB32 (ViewPortAddress (ColorWin)->ColorMap,0L,Colors,&COLOR_RGB [1L]);
  320.  
  321.                                                             RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  322.  
  323.                                                             GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  324.  
  325.                                                             BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  326.  
  327.                                                             GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  328.  
  329.                                                             GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  330.  
  331.                                                             GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  332.  
  333.                                                             break;
  334.  
  335.                                                  case 'E' : RedLevel = COLOR_RGB [ColorBase] >> 24L;
  336.  
  337.                                                             if (RedLevel > Min_Value)
  338.                                                             {
  339.                                                                KeepPalette (Win);
  340.  
  341.                                                                RedLevel--;
  342.  
  343.                                                                GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  344.  
  345.                                                                COLOR_RGB [ColorBase] = RedLevel << 24L;
  346.  
  347.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  348.                                                             }
  349.  
  350.                                                             break;
  351.  
  352.                                                  case 'T' : RedLevel = COLOR_RGB [ColorBase] >> 24L;
  353.  
  354.                                                             if (RedLevel < Max_Value)
  355.                                                             {
  356.                                                                KeepPalette (Win);
  357.  
  358.                                                                RedLevel++;
  359.  
  360.                                                                GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  361.  
  362.                                                                COLOR_RGB [ColorBase] = RedLevel << 24L;
  363.  
  364.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  365.                                                             }
  366.  
  367.                                                             break;
  368.  
  369.                                                  case 'F' : GreenLevel = COLOR_RGB [ColorBase + 1L] >> 24L;
  370.  
  371.                                                             if (GreenLevel > Min_Value)
  372.                                                             {
  373.                                                                KeepPalette (Win);
  374.  
  375.                                                                GreenLevel--;
  376.  
  377.                                                                GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  378.  
  379.                                                                COLOR_RGB [ColorBase + 1L] = GreenLevel << 24L;
  380.  
  381.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  382.                                                             }
  383.  
  384.                                                             break;
  385.  
  386.                                                  case 'H' : GreenLevel = COLOR_RGB [ColorBase + 1L] >> 24L;
  387.  
  388.                                                             if (GreenLevel < Max_Value)
  389.                                                             {
  390.                                                                KeepPalette (Win);
  391.  
  392.                                                                GreenLevel++;
  393.  
  394.                                                                GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  395.  
  396.                                                                COLOR_RGB [ColorBase + 1L] = GreenLevel << 24L;
  397.  
  398.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  399.                                                             }
  400.  
  401.                                                             break;
  402.  
  403.                                                  case 'V' : BlueLevel = COLOR_RGB [ColorBase + 2L] >> 24L;
  404.  
  405.                                                             if (BlueLevel > Min_Value)
  406.                                                             {
  407.                                                                KeepPalette (Win);
  408.  
  409.                                                                BlueLevel--;
  410.  
  411.                                                                GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  412.  
  413.                                                                COLOR_RGB [ColorBase + 2L] = BlueLevel << 24L;
  414.  
  415.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  416.                                                             }
  417.  
  418.                                                             break;
  419.  
  420.                                                  case 'N' : BlueLevel = COLOR_RGB [ColorBase + 2L] >> 24L;
  421.  
  422.                                                             if (BlueLevel < Max_Value)
  423.                                                             {
  424.                                                                KeepPalette (Win);
  425.  
  426.                                                                BlueLevel++;
  427.  
  428.                                                                GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  429.  
  430.                                                                COLOR_RGB [ColorBase + 2L] = BlueLevel << 24L;
  431.  
  432.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  433.                                                             }
  434.  
  435.                                                             break;
  436.                                          }
  437.  
  438.                                          break;
  439.  
  440.               case IDCMP_GADGETUP      : switch (MyGad->GadgetID)
  441.                                          {
  442.                                                  case ACCEPT  : GetRGB32 (ViewPortAddress (ColorWin)->ColorMap,0L,Colors,&Palette32 [1L]);
  443.  
  444.                                                                 Exit = TRUE;
  445.  
  446.                                                                 break;
  447.  
  448.                                                  case RESET   : KeepPalette (Win);
  449.  
  450.                                                                 LoadRGB32 (ViewPortAddress (ColorWin),Palette32);
  451.  
  452.                                                                 GetRGB32 (ViewPortAddress (ColorWin)->ColorMap,0L,Colors,&COLOR_RGB [1L]);
  453.  
  454.                                                                 RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  455.  
  456.                                                                 GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  457.  
  458.                                                                 BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  459.  
  460.                                                                 GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  461.  
  462.                                                                 GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  463.  
  464.                                                                 GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  465.  
  466.                                                                 break;
  467.  
  468.                                                  case CANCEL  : LoadRGB32 (ViewPortAddress (ColorWin),Palette32);
  469.  
  470.                                                                 Exit = TRUE;
  471.  
  472.                                                                 break;
  473.  
  474.                                                  case COPY    : Copy (ColorWin,SelectedPen);
  475.  
  476.                                                                 Copy_Msg = TRUE;
  477.  
  478.                                                                 Swap_Msg = FALSE;
  479.  
  480.                                                                 Spread_Msg = FALSE;
  481.  
  482.                                                                 break;
  483.  
  484.                                                  case SWAP    : OldPen = SelectedPen;
  485.  
  486.                                                                 Copy_Msg = FALSE;
  487.  
  488.                                                                 Swap_Msg = TRUE;
  489.  
  490.                                                                 Spread_Msg = FALSE;
  491.  
  492.                                                                 break;
  493.  
  494.                                                  case SPREAD  : OldPen = SelectedPen;
  495.  
  496.                                                                 Copy_Msg = FALSE;
  497.  
  498.                                                                 Swap_Msg = FALSE;
  499.  
  500.                                                                 Spread_Msg = TRUE;
  501.         
  502.                                                                 break;
  503.  
  504.                                                  case UNDO    : LoadRGB32 (ViewPortAddress (ColorWin),UNDO_RGB);
  505.  
  506.                                                                 GetRGB32 (ViewPortAddress (ColorWin)->ColorMap,0L,Colors,&COLOR_RGB [1L]);
  507.  
  508.                                                                 RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  509.  
  510.                                                                 GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  511.  
  512.                                                                 BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  513.  
  514.                                                                 GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  515.  
  516.                                                                 GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  517.  
  518.                                                                 GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  519.  
  520.                                                                 break;
  521.  
  522.                                                  case RED     : KeepPalette (Win);
  523.  
  524.                                                                 GT_GetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,&RedLevel);
  525.  
  526.                                                                 COLOR_RGB [ColorBase]      = RedLevel << 24L;
  527.  
  528.                                                                 LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  529.  
  530.                                                                 break;
  531.  
  532.                                                  case GREEN   : KeepPalette (Win);
  533.  
  534.                                                                 GT_GetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,&GreenLevel);
  535.  
  536.                                                                 COLOR_RGB [ColorBase + 1L] = GreenLevel << 24L;
  537.  
  538.                                                                 LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  539.  
  540.                                                                 break;
  541.  
  542.                                                  case BLUE    : KeepPalette (Win);
  543.  
  544.                                                                 GT_GetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,&BlueLevel);
  545.  
  546.                                                                 COLOR_RGB [ColorBase + 2L] = BlueLevel << 24L;
  547.  
  548.                                                                 LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  549.  
  550.                                                                 break;
  551.  
  552.                                                  case PALETTE : GT_GetGadgetAttrs (MyPaletteGad,ColorWin,NULL,GTPA_Color,&SelectedPen);
  553.  
  554.                                                                 if (Copy_Msg)
  555.                                                                 {
  556.                                                                    KeepPalette (Win);
  557.  
  558.                                                                    Paste (ColorWin,SelectedPen);
  559.  
  560.                                                                    Copy_Msg = FALSE;
  561.                                                                 }
  562.  
  563.                                                                 if (Swap_Msg)
  564.                                                                 {
  565.                                                                    KeepPalette (Win);
  566.  
  567.                                                                    NewPen = SelectedPen;
  568.  
  569.                                                                    Swap (ColorWin,OldPen,NewPen);
  570.  
  571.                                                                    Swap_Msg = FALSE;
  572.                                                                 }
  573.  
  574.                                                                 if (Spread_Msg)
  575.                                                                 {
  576.                                                                    KeepPalette (Win);
  577.  
  578.                                                                    NewPen = SelectedPen;
  579.  
  580.                                                                    Spread (ColorWin,OldPen,NewPen);
  581.  
  582.                                                                    Spread_Msg = FALSE;
  583.                                                                 }
  584.  
  585.                                                                 ColorBase = 3L * SelectedPen + 1L;
  586.  
  587.                                                                 RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  588.  
  589.                                                                 GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  590.  
  591.                                                                 BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  592.  
  593.                                                                 GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  594.  
  595.                                                                 GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  596.  
  597.                                                                 GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  598.  
  599.                                                                 break;
  600.                                          }
  601.  
  602.                                          break;
  603.  
  604.               case IDCMP_CLOSEWINDOW   : Exit = TRUE;
  605.  
  606.                                          break;
  607.        }
  608.  
  609.      } while (Exit == FALSE);
  610.  
  611.   CloseWindow (ColorWin);
  612.  
  613.   FreeGadgets (GadList);
  614.  
  615.   FreeVisualInfo (VInfo);
  616.  
  617.   return TRUE;
  618. }
  619.  
  620. VOID Copy (struct Window *Win,ULONG PenNumber)
  621. {
  622.   GetRGB32 (ViewPortAddress (Win)->ColorMap,PenNumber,1L,COPY_RGB);
  623. }
  624.  
  625. VOID Paste (struct Window *Win,const ULONG PenNumber)
  626. {
  627. const ULONG ColorBase = 3L * PenNumber + 1L;
  628.  
  629. ULONG Index;
  630.  
  631.   for (Index = 0L; Index < 3L; Index++) COLOR_RGB [ColorBase + Index] = COPY_RGB [Index];
  632.  
  633.   LoadRGB32 (ViewPortAddress (Win),COLOR_RGB);
  634. }
  635.  
  636. VOID Swap (struct Window *Win,const ULONG OldPen,const ULONG NewPen)
  637. {
  638. ULONG Tmp_RGB [3L];
  639.  
  640. ULONG Index;
  641.  
  642.    GetRGB32 (ViewPortAddress (Win)->ColorMap,OldPen,1L,Tmp_RGB);
  643.  
  644.    Copy (Win,NewPen);
  645.  
  646.    Paste (Win,OldPen);
  647.  
  648.    for (Index = 0L; Index < 3L; Index++) COPY_RGB [Index] = Tmp_RGB [Index];
  649.  
  650.    Paste (Win,NewPen);
  651. }
  652.  
  653. BOOL Spread (struct Window *Win,const ULONG OldPen,const ULONG NewPen)
  654. {
  655. const ULONG StartPen = min (OldPen,NewPen) , EndPen = max (OldPen,NewPen);
  656.  
  657. const ULONG  Range   = EndPen - StartPen;
  658.  
  659. LONG RedStep,GreenStep,BlueStep;
  660.  
  661. ULONG Index,RedLevel,GreenLevel,BlueLevel,ColorBase;
  662.  
  663.   if (Range < 2L) return TRUE;
  664.  
  665.   ColorBase   = 3L * StartPen + 1L;
  666.  
  667.   RedLevel    = COLOR_RGB [ColorBase]      >> 8L;
  668.  
  669.   GreenLevel  = COLOR_RGB [ColorBase + 1L] >> 8L;
  670.  
  671.   BlueLevel   = COLOR_RGB [ColorBase + 2L] >> 8L;
  672.  
  673.   ColorBase   = 3L * EndPen + 1L;
  674.  
  675.   RedStep     = (LONG) (COLOR_RGB [ColorBase]      >> 8L);
  676.  
  677.   GreenStep   = (LONG) (COLOR_RGB [ColorBase + 1L] >> 8L);
  678.  
  679.   BlueStep    = (LONG) (COLOR_RGB [ColorBase + 2L] >> 8L);
  680.  
  681.   RedStep    -= (LONG) RedLevel;
  682.  
  683.   GreenStep  -= (LONG) GreenLevel;
  684.  
  685.   BlueStep   -= (LONG) BlueLevel;
  686.  
  687.   RedStep    /= (LONG) Range;
  688.  
  689.   GreenStep  /= (LONG) Range;
  690.  
  691.   BlueStep   /= (LONG) Range;
  692.  
  693.   for (Index = (StartPen + 1L); Index < EndPen; Index++)
  694.   {
  695.       RedLevel   += RedStep;
  696.  
  697.       GreenLevel += GreenStep;
  698.  
  699.       BlueLevel  += BlueStep;
  700.  
  701.       ColorBase   = 3L * Index + 1L;
  702.  
  703.       COLOR_RGB [ColorBase]      = RedLevel   << 8L;
  704.  
  705.       COLOR_RGB [ColorBase + 1L] = GreenLevel << 8L;
  706.  
  707.       COLOR_RGB [ColorBase + 2L] = BlueLevel  << 8L;
  708.   }
  709.  
  710.   LoadRGB32 (ViewPortAddress (Win),COLOR_RGB);
  711.  
  712.   return FALSE;
  713. }
  714.  
  715. VOID KeepPalette (struct Window *Win)
  716. {
  717.    GetRGB32 (ViewPortAddress (Win)->ColorMap,0L,1L << Win->RPort->BitMap->Depth,&UNDO_RGB [1L]);
  718. }
  719.